package com.hingsmy.pmy.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hingsmy.pmy.dao.ProjectRepository;
import com.hingsmy.pmy.dao.StudentRepository;
import com.hingsmy.pmy.dto.ChartData;
import com.hingsmy.pmy.dto.StudentProject;
import com.hingsmy.pmy.entities.Project;
import com.hingsmy.pmy.entities.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
public class HomeController {

        @Value("${version}")
        private String version;
        @Autowired
        StudentRepository studentRepository;
        @Autowired
        ProjectRepository projectRepository;
        @GetMapping("/")
        public  String displayHome(Model model) throws JsonProcessingException {
                model.addAttribute("versionNumber", version);


                Map<String, Object> map = new HashMap<>();

                List<ChartData> projectStatusData = projectRepository.getProjectStatus();
                 //转换projectStatusData为json结构,方便在js脚本中使用
                ObjectMapper objectMapper = new ObjectMapper();
                String jsonString = objectMapper.writeValueAsString(projectStatusData);
                  //[{"未开始,1"}], {"进行中,2"} {"已完成,1"}
                model.addAttribute("projectStatusData",jsonString);

                List<Project> projects = projectRepository.findAll();
                model.addAttribute("projects",projects);


                List<StudentProject> studentProjects = studentRepository.studentProjects();
                model.addAttribute("studentProjects", studentProjects);

        return "main/home";
    }
}
